package com.bookmanager.www.service.impl;

import com.bookmanager.www.entity.FlBook;
import com.bookmanager.www.entity.FlCategories;
import com.bookmanager.www.mapper.FlBookMapper;
import com.bookmanager.www.mapper.FlCategoriesMapper;
import com.bookmanager.www.service.FlCategoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 图书的类别 服务实现类
 * </p>
 *
 * @author 风离
 * @since 2021-09-17
 */
@Service
public class FlCategoriesServiceImpl extends ServiceImpl<FlCategoriesMapper, FlCategories> implements FlCategoriesService {

    @Resource
    FlCategoriesMapper flCategoriesMapper;
    @Resource
    FlBookMapper flBookMapper;
    @Override
    public int deleteCategoriesByCategoriesName(String categoriesName) {

        FlCategories flCategories = flCategoriesMapper.selectCategoriesByCategoriesName(categoriesName);
        if(flCategories==null)
        {
            return 0;
        }
        Integer categoriesId = flCategories.getCategoriesId();
        //首先查询该分类下有没有图书 如果没有就直接删除该分类 如果有就先删图书
        List<FlBook> flBookList = flBookMapper.selectBookByCategoriesId(categoriesId);
        if (flBookList.size()==0)
        {
           int deleteBook = flCategoriesMapper.deleteCategoriesByCategoriesName(categoriesName);
            if (deleteBook != 0) {
                return 1;
            } else {
                return 0;
            }
        }else {
            //首先删除该类下所有图书
            int delete1 = flBookMapper.deleteBooksByCategoriesId(categoriesId);
            if (delete1!=0) {
                int delete2 = flCategoriesMapper.deleteCategoriesByCategoriesName(categoriesName);
                if (delete2 != 0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }

        return 0;
    }
}
